import React, {Component} from 'react';
import './index.less';
import {high, promptBox} from 'nc-lightapp-front';
import ReferSearch from 'src/hrpub/common/components/referSearch/org';
import do_print from "src/hrpub/common/utils/print";
import ExportHtml from "src/hrpub/common/utils/exportHtml";
import setQueryMeta from '../../container/functions/setQueryMeta';
import isMac from "../../../../../public/functions/isMac";
import checkBtnPermission from "../../container/functions/checkBtnPermission";

class Header extends Component {
    constructor(props) {
        super(props);
        this.state = {printData: {}};
        this.updateOrgValue = this.updateOrgValue.bind(this);
        this.onBtnOperation = this.onBtnOperation.bind(this);
    }

    updateOrgValue(value) {
        this.props.updateOrgValue(value);
    }

    onBtnOperation(props, btncode, areacode, opt) {
        switch (btncode) {
            case 'query':
                setQueryMeta.call(this);
                if (this.props.tabKey === 'contract') {
                    this.props.search.openAdvSearch('contractquery', true);
                } else {
                    this.props.search.openAdvSearch('agreementquery', true);
                }
                break;
            case 'refresh':
                this.props.refresh();
                break;
            case 'save':
                this.props.save();
                break;
            case 'cancel':
                promptBox({
                    color: 'warning',
                    title: this.props.json['cm6011-000000'],/* 国际化处理： 确认取消,确认取消*/
                    content: this.props.json['cm6011-000001'],/* 国际化处理： 是否确认要取消？,是否确认要取消*/
                    beSureBtnClick: () => {
                        this.props.cancel();
                    }
                });
                break;
            case 'edit':
                this.props.edit();
                break;
            case 'file':
                this.props.checkFileManage();
                /*this.props.updateState({
                    showUploader: true
                });*/
                break;
            case 'print':
                if (this.props.tabKey === 'contract') {
                    const el = document.querySelector('.contract-table').cloneNode(true);
                    const table = el.querySelector('.u-table-content');
                    table.removeChild(table.lastElementChild);
                    table.removeChild(table.lastElementChild);
                    const tHeader = el.querySelector('.u-table-header');
                    const tr = tHeader.querySelector('tr');
                    tr.removeChild(tr.firstElementChild);
                    tr.removeChild(tr.firstElementChild);
                    tr.removeChild(tr.lastElementChild);
                    tr.removeChild(tr.lastElementChild);
                    const meta = JSON.parse(JSON.stringify(this.props.meta.getMeta()['contractList']));
                    meta.items.pop();
                    meta.items.pop();
                    do_print(
                        el,
                        {
                            title: this.props.json['cm6011-000068'] /* 国际化处理： 续签意见征询*/,
                            maker: this.props.json['cm6011-000033'] /* 国际化处理： 打印人*/,
                            date: this.props.json['cm6011-000034'] /* 国际化处理： 打印日期*/
                        },
                        {
                            tableTmp: meta,
                            data: this.props.table.getAllTableData('contractList').rows
                        }
                    );
                } else {
                    const el = document.querySelector('.agree-table').cloneNode(true);
                    const table = el.querySelector('.u-table-content');
                    table.removeChild(table.lastElementChild);
                    table.removeChild(table.lastElementChild);
                    const tHeader = el.querySelector('.u-table-header');
                    const tr = tHeader.querySelector('tr');
                    tr.removeChild(tr.firstElementChild);
                    tr.removeChild(tr.firstElementChild);
                    tr.removeChild(tr.lastElementChild);
                    tr.removeChild(tr.lastElementChild);
                    const meta = JSON.parse(JSON.stringify(this.props.meta.getMeta()['agreementList']));
                    meta.items.pop();
                    meta.items.pop();
                    do_print(
                        el,
                        {
                            title: this.props.json['cm6011-000069'] /* 国际化处理： 续签意见征询*/,
                            maker: this.props.json['cm6011-000033'] /* 国际化处理： 打印人*/,
                            date: this.props.json['cm6011-000034'] /* 国际化处理： 打印日期*/
                        },
                        {
                            tableTmp: meta,
                            data: this.props.table.getAllTableData('agreementList').rows
                        }
                    );
                }
                break;
            case 'export':
                if (this.props.tabKey === 'contract') {
                    ExportHtml(document.querySelector('.contract-table'), {
                        title: this.props.json['cm6011-000068'],
                        fileName: this.props.json['cm6011-000068']
                    }, {
                        meta: this.props.meta.getMeta()['contractList'],
                        data: this.props.table.getAllTableData('contractList').rows,
                        showIndex: true
                    })
                } else {
                    ExportHtml(document.querySelector('.agree-table'), {
                        title: this.props.json['cm6011-000069'],
                        fileName: this.props.json['cm6011-000069']
                    }, {
                        meta: this.props.meta.getMeta()['agreementList'],
                        data: this.props.table.getAllTableData('agreementList').rows,
                        showIndex: true
                    })
                }
                break;
            case 'viewbooklet':
                if (!isMac(this.props.json['cm6011-000148'])) {
                    this.props.viewbooklet();
                }
                break;
            case 'viewAgree':
                if (!isMac(this.props.json['cm6011-000148'])) {
                    this.props.viewAgreeText();
                }
                break;
            case 'browse':
                if (!isMac(this.props.json['cm6011-000148'])) {
                    if (this.props.tabKey === 'contract') {
                        this.props.viewbooklet();
                    } else {
                        this.props.viewAgreeText();
                    }
                }
                break;
            case 'tempPrint':
                this.props.tempPrint();
                break;
            default:
                break;
        }
    }

    render() {
        const {button, orgVal, isEdit} = this.props;
        return (
            <div>
                <div className="header">
                    <div className="header-refer">
                        <ReferSearch disabled={isEdit} orgVal={orgVal} getOrgData={this.updateOrgValue}/>
                    </div>
                    <div className="btn-group">
                        <div className="definition-icons">
                            {button.createButtonApp({
                                area: 'head',
                                onButtonClick: (props, btncode) => {
                                    this.onBtnOperation(props, btncode, 'head', this);
                                }
                            })}
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}

export default Header;
